package com.tusuo.stds.ui.vip;

import android.content.Context;
import android.content.Intent;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.AttachDataResult;
import com.tusuo.stds.rest.UserInfo;
import com.tusuo.stds.rest.VipList;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;

/**
 * Created by developer on 17-8-29.
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(VipPresenter.class)
public class VipPresenter extends MvpBasePresenter<VipView> {

    @Inject
    ApiHelper mApiHelper;

    private Context mContext;
    private UserInfo mUserInfo;

    VipPresenter(Context context) {
        mContext = context;

        Injector.initVipPresenterComponent().inject(this);
    }


    @Override
    public void attachView(VipView view) {
        super.attachView(view);
    }

    @Override
    public void detachView(boolean retainInstance) {
        super.detachView(retainInstance);
    }

    void initUserInfo() {
        mApiHelper.getUserInfo(new GetUserInfoSubscriber());
    }

    void getVipList() {
        mApiHelper.getVipList(new GetVipListSubscriber());
    }


    private class GetUserInfoSubscriber extends Subscriber<UserInfo> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(UserInfo userInfo) {
            if (userInfo != null) {
                mUserInfo = userInfo;
                getVipList();
            }
        }
    }

    private class GetVipListSubscriber extends Subscriber<VipList> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            System.out.println("error" + e);

        }

        @Override
        public void onNext(VipList vipList) {
            if (vipList.Data != null) {
                if (getView() != null) {
                    getView().initVipData(mUserInfo, vipList);
                }
            }
        }
    }



}
